package com.FH.organization.LinesNodes;

import com.FH.organization.manage_line.ManageLinePullDTO;
import com.FH.organization.manage_line.ManageLineResource;
import com.FH.organization.manage_node.ManageNodeResource;
import com.FH.organization.manage_node.ManageNodePullDTO;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/9.
 */
@Stateless
public class LinesNodesDAOImpl extends AccessDB implements LinesNodesDAO {


    @Inject
    ManageLineResource manageLineResource;

    @Inject
    ManageNodeResource manageNodeResource;

    /**
     * 添加集合信息
     *
     * @param linesNodesPullDTO
     * @return
     */
    @Override
    public ResultDTO addLinesNodesInfo(LinesNodesPullDTO linesNodesPullDTO) {

        ResultDTO resultDTO = new ResultDTO();
        String manage_id = linesNodesPullDTO.getManage_id();
        ResultDTO nodeslist;
        nodeslist = manageNodeResource.getManageNodeInfo("{manage_id:" + manage_id + "}", "0", "9999999");
        if (nodeslist.getValues().size() > 0) {
            //删除节点
            ManageNodePullDTO manageNodePullDTO = new ManageNodePullDTO();
            manageNodePullDTO.setManage_id(manage_id);
            manageNodeResource.deleteManageNodeInfo(manageNodePullDTO);

        }
        /**
         * JSON解析线表集合
         */
        if (null != linesNodesPullDTO.getLines() && !"".equals(linesNodesPullDTO.getLines())) {
            String lines = linesNodesPullDTO.getLines();
            try {
                JSONArray array = new JSONArray(lines);
                if (array != null && array.length() > 0) {
                    List<ManageLinePullDTO> result = new ArrayList<ManageLinePullDTO>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject jsonObject = array.getJSONObject(i);
                        String type = jsonObject.getString("type");
                        String sourceId = jsonObject.getString("sourceId");
                        String targetId = jsonObject.getString("targetId");
                        String line_desc = jsonObject.getString("line_desc");

                        ManageLinePullDTO manageLinePullDTO = new ManageLinePullDTO();
                        manageLinePullDTO.setManage_id(manage_id);
                        manageLinePullDTO.setType(type);
                        manageLinePullDTO.setSourceId(sourceId);
                        manageLinePullDTO.setTargetId(targetId);
                        manageLinePullDTO.setLine_desc(line_desc);
                        manageLineResource.addManageLineInfo(manageLinePullDTO);
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        /**
         * JSON解析节点信息
         */
        if (null != linesNodesPullDTO.getNodes() && !"".equals(linesNodesPullDTO.getNodes())) {
            String nodes = linesNodesPullDTO.getNodes();
            try {
                JSONArray array = new JSONArray(nodes);
                if (array != null && array.length() > 0) {
                    List<ManageNodePullDTO> result = new ArrayList<ManageNodePullDTO>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject jsonObject = array.getJSONObject(i);
                        String nodeid = jsonObject.getString("nodeid");
                        String nodetext = jsonObject.getString("nodetext");
                        String node_x = jsonObject.getString("node_x");
                        String node_y = jsonObject.getString("node_y");
                        String organization_id = jsonObject.getString("organization_id");
                        String member_id = jsonObject.getString("member_id");
                        String step = jsonObject.getString("step");
                        String node_state_info = jsonObject.getString("node_state_info");


                        ManageNodePullDTO manageNodePullDTO = new ManageNodePullDTO();
                        manageNodePullDTO.setManage_id(manage_id);
                        manageNodePullDTO.setNodeid(nodeid);
                        manageNodePullDTO.setNodetext(nodetext);
                        manageNodePullDTO.setNode_x(node_x);
                        manageNodePullDTO.setNode_y(node_y);
                        manageNodePullDTO.setOrganization_id(organization_id);
                        manageNodePullDTO.setMember_id(member_id);
                        manageNodePullDTO.setStep(step);
                        manageNodePullDTO.setNode_state_info(node_state_info);
                        manageNodeResource.addManageNodeInfo(manageNodePullDTO);
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        resultDTO.setStatus("90000");
        resultDTO.setDescritpion("流程添加成功！");

        return resultDTO;
    }

    /**
     * 查询两个集合信息
     *
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getLinesNodesInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);


            if (json.has("manage_id")) {

                String manage_id = json.getString("manage_id");
                List<LinesNodesShowDTO> line_nodes = new ArrayList<LinesNodesShowDTO>();
                LinesNodesShowDTO linesNodesShowDTO = new LinesNodesShowDTO();
                ResultDTO nodeslist;
                nodeslist = manageNodeResource.getManageNodeInfo("{manage_id:" + manage_id + "}", "0", "9999999");
                if(nodeslist.getValues()!=null&&nodeslist.getValues().size()>0){
                    linesNodesShowDTO.setNodes(nodeslist.getValues());
                }

                ResultDTO lineslist;
                lineslist = manageLineResource.getManageLineInfo("{manage_id:" + manage_id + "}", "0", "9999999");
                if(lineslist.getValues()!=null&&lineslist.getValues().size()>0){
                    linesNodesShowDTO.setLines(lineslist.getValues());
                }
                line_nodes.add(linesNodesShowDTO);
                resultDTO.setValues(line_nodes);

            }
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.get_success);


        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resultDTO;
    }

}